﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class groupIndex_Billboard : System.Web.UI.Page
{
    private string groupID;

    protected void Page_Load(object sender, EventArgs e)
    {
        //检查是否登录
        if (Request.Cookies["userID"] == null)
        {
            Response.Redirect("notLogin.html", true);
            return;
        }
        else if (Request.QueryString["groupID"] == null)
        {
            //检查GET参数是否齐全
            Response.Redirect("noGETParameter.html", true);
            return;
        }
        else
        {
            groupID = Request.QueryString["groupID"];

            if (!IsPostBack)
            {
                //获取小组公告板内容
                ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["connectionString"];
                using (SqlConnection dataBaseConnection = new SqlConnection(settings.ConnectionString))
                {
                    dataBaseConnection.Open();


                    string queryString = "SELECT Billboard " +
                                         "FROM GroupInfo " +
                                         "WHERE GroupID = @GroupID";
                    SqlCommand command = new SqlCommand(queryString, dataBaseConnection);
                    SqlParameter GroupIDParameter = new SqlParameter("@GroupID", groupID);
                    command.Parameters.Add(GroupIDParameter);
                    SqlDataReader reader;
                    try
                    {
                        reader = command.ExecuteReader();
                    }
                    catch (Exception exception)
                    {
                        billboard.Text = "数据库方面出现了错误，请联系我们以解决 AFTER GET Billboard " + exception.Message;
                        billboard.Style["background-color"] = "#220000";
                        edit.Enabled = false;
                        cancel.Enabled = false;
                        submit.Enabled = false;
                        return;
                    }

                    reader.Read();
                    string billboardFromDB = reader["Billboard"].ToString();
                    reader.Close();

                    billboard.Text = billboardFromDB;
                    dataBaseConnection.Close();
                }
            }
        }
    }
    protected void submit_Click(object sender, EventArgs e)
    {
        //将新的公告放入数据库
        ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["connectionString"];
        using (SqlConnection dataBaseConnection = new SqlConnection(settings.ConnectionString))
        {
            dataBaseConnection.Open();

            string queryString = "UPDATE GroupInfo " +
                                 "SET Billboard = @Billboard " +
                                 "WHERE GroupID = @GroupID";
            SqlCommand command = new SqlCommand(queryString, dataBaseConnection);
            SqlParameter BillboardParameter = new SqlParameter("@Billboard", billboard.Text);
            SqlParameter GroupIDParameter = new SqlParameter("@GroupID", groupID);
            command.Parameters.AddRange(new SqlParameter[]{BillboardParameter, GroupIDParameter});
            try
            {
                command.ExecuteNonQuery();
            }
            catch (Exception exception)
            {
                billboard.Text = "数据库方面出现了错误，请联系我们以解决 AFTER UPDATE Billboard " + exception.Message;
                billboard.Style["background-color"] = "#220000";
                edit.Enabled = false;
                cancel.Enabled = false;
                submit.Enabled = false;
                return;
            }

            dataBaseConnection.Close();
        }
    }
}